package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.domain.WmsDeliveryPlan;
import com.ruoyi.business.domain.WmsDeliveryRecord;
import com.ruoyi.business.mapper.WmsDeliveryPlanMapper;
import com.ruoyi.business.mapper.WmsDeliveryRecordMapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.WmsDeliveryRecordDetailMapper;
import com.ruoyi.business.domain.WmsDeliveryRecordDetail;
import com.ruoyi.business.service.IWmsDeliveryRecordDetailService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 出库单货物信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-13
 */
@Service
public class WmsDeliveryRecordDetailServiceImpl implements IWmsDeliveryRecordDetailService
{
    @Autowired
    private WmsDeliveryRecordDetailMapper wmsDeliveryRecordDetailMapper;

    @Autowired
    private WmsDeliveryRecordMapper wmsDeliveryRecordMapper;

    @Autowired
    private WmsDeliveryPlanMapper wmsDeliveryPlanMapper;

    /**
     * 查询出库单货物信息
     *
     * @param id 出库单货物信息主键
     * @return 出库单货物信息
     */
    @Override
    public WmsDeliveryRecordDetail selectWmsDeliveryRecordDetailById(Long id)
    {
        return wmsDeliveryRecordDetailMapper.selectWmsDeliveryRecordDetailById(id);
    }

    /**
     * 查询出库单货物信息列表
     *
     * @param wmsDeliveryRecordDetail 出库单货物信息
     * @return 出库单货物信息
     */
    @Override
    public List<WmsDeliveryRecordDetail> selectWmsDeliveryRecordDetailList(WmsDeliveryRecordDetail wmsDeliveryRecordDetail)
    {
        return wmsDeliveryRecordDetailMapper.selectWmsDeliveryRecordDetailList(wmsDeliveryRecordDetail);
    }

    /**
     * 新增出库单货物信息
     *
     * @param wmsDeliveryRecordDetail 出库单货物信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertWmsDeliveryRecordDetail(WmsDeliveryRecordDetail wmsDeliveryRecordDetail)
    {
        wmsDeliveryRecordDetail.setCreateTime(DateUtils.getNowDate());
        int i = wmsDeliveryRecordDetailMapper.insertWmsDeliveryRecordDetail(wmsDeliveryRecordDetail);
        // 更新出库状态
        WmsDeliveryRecord wmsDeliveryRecord = wmsDeliveryRecordMapper.selectWmsDeliveryRecordByDeliveryNo(wmsDeliveryRecordDetail.getDeliveryNo());
        Long planId = wmsDeliveryRecord.getPlanId();
        WmsDeliveryPlan plan = wmsDeliveryPlanMapper.selectWmsDeliveryPlanById(planId);
        Integer status = wmsDeliveryRecordMapper.queryStatus(planId, plan.getDeliveryNo());
        String deliveryStatus = "partial";
        if (status == null || status == 0) {
            deliveryStatus = "all";
        }
        WmsDeliveryPlan deliveryPlan = new WmsDeliveryPlan();
        deliveryPlan.setId(planId);
        deliveryPlan.setStatus(deliveryStatus);
        wmsDeliveryPlanMapper.updateWmsDeliveryPlan(deliveryPlan);
        WmsDeliveryRecord deliveryRecord = new WmsDeliveryRecord();
        deliveryRecord.setId(wmsDeliveryRecord.getId());
        deliveryRecord.setStatus(deliveryStatus);
        wmsDeliveryRecordMapper.updateWmsDeliveryRecord(deliveryRecord);
        return i;
    }

    /**
     * 修改出库单货物信息
     *
     * @param wmsDeliveryRecordDetail 出库单货物信息
     * @return 结果
     */
    @Override
    public int updateWmsDeliveryRecordDetail(WmsDeliveryRecordDetail wmsDeliveryRecordDetail)
    {
        wmsDeliveryRecordDetail.setUpdateTime(DateUtils.getNowDate());
        int i = wmsDeliveryRecordDetailMapper.updateWmsDeliveryRecordDetail(wmsDeliveryRecordDetail);
        // 更新出库状态
        WmsDeliveryRecord wmsDeliveryRecord = wmsDeliveryRecordMapper.selectWmsDeliveryRecordByDeliveryNo(wmsDeliveryRecordDetail.getDeliveryNo());
        Long planId = wmsDeliveryRecord.getPlanId();
        WmsDeliveryPlan plan = wmsDeliveryPlanMapper.selectWmsDeliveryPlanById(planId);
        Integer status = wmsDeliveryRecordMapper.queryStatus(planId, plan.getDeliveryNo());
        String deliveryStatus = "partial";
        if (status == null || status == 0) {
            deliveryStatus = "all";
        }
        WmsDeliveryPlan deliveryPlan = new WmsDeliveryPlan();
        deliveryPlan.setId(planId);
        deliveryPlan.setStatus(deliveryStatus);
        wmsDeliveryPlanMapper.updateWmsDeliveryPlan(deliveryPlan);
        WmsDeliveryRecord deliveryRecord = new WmsDeliveryRecord();
        deliveryRecord.setId(wmsDeliveryRecord.getId());
        deliveryRecord.setStatus(deliveryStatus);
        wmsDeliveryRecordMapper.updateWmsDeliveryRecord(deliveryRecord);
        return i;
    }

    @Override
    public int chuku(WmsDeliveryRecordDetail wmsDeliveryRecordDetail) {
        WmsDeliveryRecordDetail detail = wmsDeliveryRecordDetailMapper.selectWmsDeliveryRecordDetail(wmsDeliveryRecordDetail);
        if (detail == null) {
            throw new ServiceException("没有对应出库信息");
        }
        detail.setUpdateTime(DateUtils.getNowDate());
        detail.setUpdateBy(wmsDeliveryRecordDetail.getUpdateBy());
        Long num = wmsDeliveryRecordDetail.getNum();
        Long num1 = detail.getNum();
        if (num == null) {
            num = 0L;
        }
        detail.setNum(num + num1);
        int i = wmsDeliveryRecordDetailMapper.updateWmsDeliveryRecordDetail(detail);
        // 更新出库状态
        WmsDeliveryRecord wmsDeliveryRecord = wmsDeliveryRecordMapper.selectWmsDeliveryRecordByDeliveryNo(wmsDeliveryRecordDetail.getDeliveryNo());
        Long planId = wmsDeliveryRecord.getPlanId();
        WmsDeliveryPlan plan = wmsDeliveryPlanMapper.selectWmsDeliveryPlanById(planId);
        Integer status = wmsDeliveryRecordMapper.queryStatus(planId, plan.getDeliveryNo());
        String deliveryStatus = "partial";
        if (status == null || status == 0) {
            deliveryStatus = "all";
        }
        WmsDeliveryPlan deliveryPlan = new WmsDeliveryPlan();
        deliveryPlan.setId(planId);
        deliveryPlan.setStatus(deliveryStatus);
        wmsDeliveryPlanMapper.updateWmsDeliveryPlan(deliveryPlan);
        WmsDeliveryRecord deliveryRecord = new WmsDeliveryRecord();
        deliveryRecord.setId(wmsDeliveryRecord.getId());
        deliveryRecord.setStatus(deliveryStatus);
        wmsDeliveryRecordMapper.updateWmsDeliveryRecord(deliveryRecord);
        return i;
    }

    /**
     * 批量删除出库单货物信息
     *
     * @param ids 需要删除的出库单货物信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsDeliveryRecordDetailByIds(Long[] ids)
    {
        Long id = ids[0];
        WmsDeliveryRecordDetail detail = wmsDeliveryRecordDetailMapper.selectWmsDeliveryRecordDetailById(id);
        int i = wmsDeliveryRecordDetailMapper.deleteWmsDeliveryRecordDetailByIds(ids);
        // 更新出库状态
        WmsDeliveryRecordDetail query = new WmsDeliveryRecordDetail();
        query.setDeliveryNo(detail.getDeliveryNo());
        List<WmsDeliveryRecordDetail> details = wmsDeliveryRecordDetailMapper.selectWmsDeliveryRecordDetailList(query);
        String deliveryStatus = "partial";
        if (ObjectUtils.isEmpty(details)) {
            deliveryStatus = "wait";
        }
        WmsDeliveryRecord wmsDeliveryRecord = wmsDeliveryRecordMapper.selectWmsDeliveryRecordByDeliveryNo(detail.getDeliveryNo());
        Long planId = wmsDeliveryRecord.getPlanId();
        WmsDeliveryPlan plan = wmsDeliveryPlanMapper.selectWmsDeliveryPlanById(planId);
        Integer status = wmsDeliveryRecordMapper.queryStatus(planId, plan.getDeliveryNo());
        if (status == null || status == 0) {
            deliveryStatus = "all";
        }
        WmsDeliveryPlan deliveryPlan = new WmsDeliveryPlan();
        deliveryPlan.setId(planId);
        deliveryPlan.setStatus(deliveryStatus);
        wmsDeliveryPlanMapper.updateWmsDeliveryPlan(deliveryPlan);
        WmsDeliveryRecord deliveryRecord = new WmsDeliveryRecord();
        deliveryRecord.setId(wmsDeliveryRecord.getId());
        deliveryRecord.setStatus(deliveryStatus);
        wmsDeliveryRecordMapper.updateWmsDeliveryRecord(deliveryRecord);
        return i;
    }

    /**
     * 删除出库单货物信息信息
     *
     * @param id 出库单货物信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsDeliveryRecordDetailById(Long id)
    {
        WmsDeliveryRecordDetail detail = wmsDeliveryRecordDetailMapper.selectWmsDeliveryRecordDetailById(id);
        int i = wmsDeliveryRecordDetailMapper.deleteWmsDeliveryRecordDetailById(id);
        // 更新出库状态
        WmsDeliveryRecordDetail query = new WmsDeliveryRecordDetail();
        query.setDeliveryNo(detail.getDeliveryNo());
        List<WmsDeliveryRecordDetail> details = wmsDeliveryRecordDetailMapper.selectWmsDeliveryRecordDetailList(query);
        String deliveryStatus = "partial";
        if (ObjectUtils.isEmpty(details)) {
            deliveryStatus = "wait";
        }
        WmsDeliveryRecord wmsDeliveryRecord = wmsDeliveryRecordMapper.selectWmsDeliveryRecordByDeliveryNo(detail.getDeliveryNo());
        Long planId = wmsDeliveryRecord.getPlanId();
        WmsDeliveryPlan plan = wmsDeliveryPlanMapper.selectWmsDeliveryPlanById(planId);
        Integer status = wmsDeliveryRecordMapper.queryStatus(planId, plan.getDeliveryNo());
        if (status == null || status == 0) {
            deliveryStatus = "all";
        }
        WmsDeliveryPlan deliveryPlan = new WmsDeliveryPlan();
        deliveryPlan.setId(planId);
        deliveryPlan.setStatus(deliveryStatus);
        wmsDeliveryPlanMapper.updateWmsDeliveryPlan(deliveryPlan);
        WmsDeliveryRecord deliveryRecord = new WmsDeliveryRecord();
        deliveryRecord.setId(wmsDeliveryRecord.getId());
        deliveryRecord.setStatus(deliveryStatus);
        wmsDeliveryRecordMapper.updateWmsDeliveryRecord(deliveryRecord);
        return i;
    }
}
